﻿using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.RegularExpressions;

namespace DSpider.Utilities
{
    public class Tools
    {
        public static string NoHTML(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "",
            RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
            RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "",
             RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&",
             RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<",
             RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">",
                RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ",
               RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2",
             RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3",
             RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9",
                RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "",
              RegexOptions.IgnoreCase);

            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            Htmlstring = Htmlstring.Trim();

            return Htmlstring;
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="package"></param>
        /// <param name="index">sheet索引</param>
        /// <param name="re">替换内容成空字符</param>
        /// <returns></returns>
        public static T readExcelPackageToString<T>(ExcelPackage package, int index, string re = " ") where T : class
        {
            try
            {
                var worksheet = package.Workbook.Worksheets[index];
                var rowCount = worksheet.Dimension?.Rows;
                var colCount = worksheet.Dimension?.Columns;

                var sb = new StringBuilder();
                sb.Append("[");
                for (int row = 1; row < rowCount.Value; row++)
                {
                    var model = "{";
                    var jugde = true;
                    for (int col = 1; col <= colCount.Value; col++)
                    {

                        var value = package.Workbook.Worksheets[index].Cells[row + 1, col].Value?.ToString() ?? "";
                        value = value.Trim().Replace(re, "");
                        if (value.Contains(@""""))
                        {
                            value = value.Replace(@"""",@"\\\""");
                        }
                        if (!string.IsNullOrWhiteSpace(value))
                        {
                            jugde = false;
                        }
                        model += "\"" + package.Workbook.Worksheets[index].Cells[1, col].Value + "\"" + ":" + "\"" + value + "\"";
                        //如果是最后一个元素则不加逗号
                        if (col != colCount.Value)
                        {
                            model += ",";
                        }

                    }

                    //如果是最后一个元素则不加逗号
                    if (row + 1 != rowCount.Value)
                    {
                        model += "}" + ",";
                    }
                    else
                    {
                        model += "}";
                    }
                    if (jugde)
                    {
                        break;
                    }
                    sb.Append(model);
                }
                sb.Append("]");
                return JsonConvert.DeserializeObject<T>(sb.ToString());
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="package"></param>
        /// <param name="index">sheet索引</param>
        /// <param name="rowStartIndex">row开始行号（表头）</param>
        /// <returns></returns>
        public static T readExcelPackageToString<T>(ExcelPackage package, int index, int rowStartIndex) where T : class
        {
            try
            {
                var worksheet = package.Workbook.Worksheets[index];
                var rowCount = worksheet.Dimension?.Rows;
                var colCount = worksheet.Dimension?.Columns;

                var sb = new StringBuilder();
                sb.Append("[");
                for (int row = rowStartIndex; row < rowCount.Value; row++)
                {
                    sb.Append("{");
                    for (int col = 1; col <= colCount.Value; col++)
                    {

                        sb.Append("\"" + package.Workbook.Worksheets[index].Cells[rowStartIndex, col].Value + "\"" + ":" + "\"" + package.Workbook.Worksheets[index].Cells[row + 1, col].Value + "\"");
                        //如果是最后一个元素则不加逗号
                        if (col != colCount.Value)
                        {
                            sb.Append(",");
                        }

                    }
                    //如果是最后一个元素则不加逗号
                    if (row + 1 != rowCount.Value)
                    {
                        sb.Append("}" + ",");
                    }
                    else
                    {
                        sb.Append("}");
                    }
                }
                sb.Append("]");
                return JsonConvert.DeserializeObject<T>(sb.ToString());
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }


    }
}
